
#include <Arduino.h>

const char index_html[] PROGMEM = R"rawliteral(  
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>WiFi接入</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            padding: 20px;
            background-color: #fff;
        }

        .input-group {
            margin-bottom: 15px;
        }

        .input-group label {
            display: block;
            margin-bottom: 5px;
        }

        .input-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .button:hover {
            background-color: #45a049;
        }

        .error-message {
            color: red;
            font-size: 14px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2>接入WiFi</h2>
        <div class="input-group">
            <label for="username">WiFi名:</label>
            <input type="text" id="username" placeholder="请输入WiFi名">
        </div>
        <div class="input-group">
            <label for="password">WiFi密码:</label>
            <input type="password" id="password" placeholder="请输入WiFi密码">
        </div>
        <div class="input-group">
            <button class="button" onclick="login()">连接</button>
        </div>
        <div class="error-message" id="error-message"></div>
    </div>

    <script>
        function login() {
            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;
            if (!username || !password) {
                document.getElementById('error-message').textContent = 'Wifi名和密码不能为空！';
                return;
            }
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://192.168.4.1', true); // 假设后端处理登录的脚本是 login.php  
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var response = JSON.parse(xhr.responseText);
                    if (response.success) {
                        // 登录成功，可以进行页面跳转或显示成功消息  
                        document.getElementById('error-message').textContent = response.message;
                        alert(response.message);
                    } else {
                        // 登录失败，显示错误信息  
                        document.getElementById('error-message').textContent = response.message;
                        alert(response.message);
                    }
                }
            };
            xhr.send('ssid=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password));
        }  
    </script>
</body>

</html>
)rawliteral";